package com.atguigu.realtime.suger.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.realtime.suger.bean.Keyword;
import com.atguigu.realtime.suger.bean.Product;
import com.atguigu.realtime.suger.bean.ProvinceStats;
import com.atguigu.realtime.suger.bean.VisitorStats;
import com.atguigu.realtime.suger.service.KeywordService;
import com.atguigu.realtime.suger.service.ProductService;
import com.atguigu.realtime.suger.service.ProvinceService;
import com.atguigu.realtime.suger.service.VisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/11/6 10:15
 */

@RestController
public class SugarController {
    
    @Autowired
    ProductService product;
    
    // ..:8070/sugar/gmv
    @RequestMapping("/sugar/gmv")
    public String gmv(@RequestParam(value = "date", defaultValue = "0") int date) {
        
        date = defaultDate(date);
        
        BigDecimal gmv = product.gmv(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        result.put("data", gmv);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/gmv/tm")
    public String gmvTm(@RequestParam(value = "date", defaultValue = "0") int date) {
        
        date = defaultDate(date);
        
        List<Map<String, Object>> list = product.gmvByTmName(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        JSONArray categories = new JSONArray();
        
        for (Map<String, Object> map : list) {
            String tmName = (String) map.get("tm_name");
            categories.add(tmName);
        }
        
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        JSONObject obj = new JSONObject();
        obj.put("name", "商品品牌");
        JSONArray data1 = new JSONArray();
        for (Map<String, Object> map : list) {
            BigDecimal orderAmount = (BigDecimal) map.get("order_amount");
            data1.add(orderAmount);
        }
        obj.put("data", data1);
        
        series.add(obj);
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/gmv/c3")
    public String gmvC3(@RequestParam(value = "date", defaultValue = "0") int date) {
        
        date = defaultDate(date);
        
        List<Product> list = product.gmvByC3(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (Product p : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", p.getCategory3_name());
            obj.put("value", p.getOrder_amount());
            data.add(obj);
        }
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @RequestMapping("/sugar/gmv/spu")
    public String gmvSpu(@RequestParam(value = "date", defaultValue = "0") int date) {
        
        date = defaultDate(date);
        
        List<Product> list = product.gmvBySpu(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray columns = new JSONArray();
        
        JSONObject c1 = new JSONObject();
        c1.put("name", "spu名字");
        c1.put("id", "spu_name");
        columns.add(c1);
        
        JSONObject c2 = new JSONObject();
        c2.put("name", "销售额");
        c2.put("id", "order_amount");
        columns.add(c2);
        
        data.put("columns", columns);
        
        JSONArray rows = new JSONArray();
        
        for (Product p : list) {
            JSONObject row = new JSONObject();
            row.put("spu_name", p.getSpu_name());
            row.put("order_amount", p.getOrder_amount());
            rows.add(row);
        }
        
        data.put("rows", rows);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    ProvinceService provinceService;
    
    @RequestMapping("/sugar/map/province")
    public String province(@RequestParam(value = "date", defaultValue = "0") int date) {
        
        date = defaultDate(date);
        
        List<ProvinceStats> list = provinceService.province(date);
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray mapData = new JSONArray();
        // TODO
        for (ProvinceStats ps : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", ps.getProvince_name());
            obj.put("value", ps.getOrder_amount());
            
            JSONArray tooltipValues = new JSONArray();
            tooltipValues.add(ps.getOrder_count());
            obj.put("tooltipValues", tooltipValues);
            
            mapData.add(obj);
        }
        data.put("mapData", mapData);
        
        data.put("valueName", "销售额");
        
        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("订单数");
        data.put("tooltipNames", tooltipNames);
        
        JSONArray tooltipUnits = new JSONArray();
        tooltipUnits.add("个");
        data.put("tooltipUnits", tooltipUnits);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    @Autowired
    VisitorService visitorService;
    
    @RequestMapping("/sugar/visitor/hour")
    public String visitorHour(@RequestParam(value = "date", defaultValue = "0") int date) {
        
        date = defaultDate(date);
        
        List<VisitorStats> list = visitorService.statsByHour(date);
        
        HashMap<Integer, VisitorStats> hourToVsMap = new HashMap<>();
        for (VisitorStats vs : list) {
            hourToVsMap.put(vs.getHour(), vs);
        }
        
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (int i = 0; i < 24; i++) {
            categories.add(i);
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject uv = new JSONObject();
        uv.put("name", "独立访客数");
        JSONArray uvData = new JSONArray();
        uv.put("data", uvData);
        series.add(uv);
        
        JSONObject pv = new JSONObject();
        pv.put("name", "页面访问数");
        JSONArray pvData = new JSONArray();
        pv.put("data", pvData);
        series.add(pv);
        
        JSONObject uj = new JSONObject();
        uj.put("name", "用户跳出数");
        JSONArray ujData = new JSONArray();
        uj.put("data", ujData);
        series.add(uj);
        
        for (int hour = 0; hour < 24; hour++) {
            // 0 点: 获取对应的uv
            VisitorStats vs = hourToVsMap.getOrDefault(hour, new VisitorStats(hour, 0L, 0L, 0L));
            uvData.add(vs.getUv());
            pvData.add(vs.getPv());
            ujData.add(vs.getUj());
        }
        
        data.put("series", series);
        
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    
    @Autowired
    KeywordService kw;
    
    @RequestMapping("/sugar/kw")
    public String kw(@RequestParam(value = "date", defaultValue = "0") int date) {
        
        date = defaultDate(date);
    
        List<Keyword> list = this.kw.kw(date);
    
        JSONObject result = new JSONObject();
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
    
        for (Keyword word : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", word.getKeyword());
            obj.put("value", word.getScore());
    
            data.add(obj);
        }
        
       
        result.put("data", data);
        
        return result.toJSONString();
        
    }
    
    private int defaultDate(@RequestParam(value = "date", defaultValue = "0") int date) {
        if (date == 0) { // 表示没有传递日期, 使用当前的日期
            return Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(System.currentTimeMillis()));
        }
        return date;
    }
}
